/*
 * Daniel R Padilla
 *
 * Copyright (c) 2008, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.select;

import java.util.ArrayList;

import org.databasefacade.table.Table;

/**
 * Conditions are ansiSQL-92 standard so this class
 * like the operator class do not require any
 * abstract overrides. The class is implemented 
 * as a ansiSQL-92 compatible.
 * 
 * @author Daniel
 *
 */
public class Condition {
	protected static final String AND = "AND";
	protected static final String OR = "OR";
	
	protected ArrayList comparisons = new ArrayList();
	protected ArrayList conditions = new ArrayList();
	
	public boolean isEmpty() {
		return (comparisons.size() == 0);
	}
	
	public int size() {
		return conditions.size();
	}
	
	public Comparison and() {
		Comparison comp = new Comparison();
		and(comp);
		return comp;
	}
	
	public Condition and(Comparison compare) {
		comparisons.add(compare);
		conditions.add(AND);
		return this;
	}
	
	public Condition and(Condition innerCondition) {
		if ( innerCondition.size() == 0 ) {
			return this;
		}
		comparisons.add(innerCondition);
		conditions.add(AND);
		return this;
	}
	
	
	public Comparison or() {
		Comparison comp = new Comparison();
		or(comp);
		return comp;
	}
	
	public Condition or(Comparison compare) {
		comparisons.add(compare);
		conditions.add(OR);
		return this;
	}
	
	public Condition or(Condition innerCondition) {
		if ( innerCondition.size() == 0 ) {
			return this;
		}
		comparisons.add(innerCondition);
		conditions.add(OR);
		return this;
	}
		
	public String getSQL() {
		StringBuilder sql= new StringBuilder("");
		for (int i=0; i < comparisons.size(); i++) {
			Object comparison = comparisons.get(i);
			String conditionType = (String) conditions.get(i);
			String compSQL=null;
			if ( comparison instanceof org.databasefacade.select.Comparison ) {
				Comparison comp = (Comparison) comparison;
				compSQL = comp.getSQL();
			} else {
				Condition con = (Condition) comparison;
				compSQL = con.getSQL();
			}
			sql.append("\n   " + conditionType + " (" + compSQL + ")");
		}
		return sql.toString();
	}	
	
	public String toString() {
		return getSQL();
	}
	
	public String getNativeSQL() {
		StringBuilder sql= new StringBuilder("");
		for (int i=0; i < comparisons.size(); i++) {
			Object comparison = comparisons.get(i);
			String conditionType = (String) conditions.get(i);
			String compSQL=null;
			if ( comparison instanceof org.databasefacade.select.Comparison ) {
				Comparison comp = (Comparison) comparison;
				compSQL = comp.getNativeSQL();
			} else {
				Condition con = (Condition) comparison;
				compSQL = con.getNativeSQL();
			}
			sql.append("\n   " + conditionType + "(" + compSQL + ")");
		}
		return sql.toString();
	}
	
	public ArrayList getAllComparisons() {
		ArrayList compList = new ArrayList();
		for (int i=0; i < comparisons.size(); i++) {
			Object comparison = comparisons.get(i);
			if ( comparison instanceof org.databasefacade.select.Comparison ) {
				Comparison comp = (Comparison) comparison;
				compList.add(comp);
			} else {
				Condition con = (Condition) comparison;
				ArrayList cl2 = con.getAllComparisons();
				for (int c= 0; c < cl2.size(); c++) {
					Comparison comp = (Comparison) cl2.get(c);
					compList.add(comp);
				}
			}
		}
		return compList;
	}
	
	public Condition buildExclusiveForTable(Table table) {
		Condition exclusive=new Condition();
		for (int i=0; i < comparisons.size(); i++) {
			Object comparison = comparisons.get(i);
			if ( comparison instanceof org.databasefacade.select.Comparison ) {
				Comparison comp = (Comparison) comparison;
				if ( comp.isRestrictedToTable(table) ) {
					if ( conditions.get(i).toString().equals(AND) ) {
						and(comp);
					} else {
						or(comp);
					}
				}
			} else {
				if ( conditions.get(i).toString().equals(AND) ) {
					and(buildExclusiveForTable(table));
				} else {
					or(buildExclusiveForTable(table));
				}
			}
		}
		return exclusive;
	}
}
